<?php
namespace youwen\exwechat;

class exWechat
{
    
    private $_token = '';
    public function __construct($token='')
    {
        if (!empty($token)) {
            $this->_token = $token;
        }
    }

    /**
     * 验证
     */
    public function authentication()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $tmpArr = array($this->_token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $sign = sha1($tmpStr);
        if ($sign == $signature) {
            if(isset($_GET['echostr'])){
                return $_GET['echostr'];
            }
            return true;
        }
        return false;
    }

    /**
     * 验证消息是否来自微信服务器
     * @return [type] [description]
     * @author baiyouwen
     */
    public function checkIP($ip)
    {
        if(in_array($ip, ['127.0.0.1'])){
            return true;
        }
        if(in_array($ip, $this->ip_list)){
            return true;
        }
        if($this->_in_ips($ip)){
            return true;
        }
        return false;
    }

    private function _in_ips($ip)
    {
        $num = strrpos($ip, '.');
        $prefix = substr($ip, 0, $num);
        $postfix = substr($ip, $num+1);
        foreach ($this->ips_list as $value) {
            if($prefix == substr($value, 0, $num)){
                $arr = explode('/', substr($value, $num+1));
                sort($arr, SORT_NUMERIC);
                if( $arr[1] > $postfix && $postfix > $arr[0] || $arr[1]==$postfix || $arr[0] == $postfix){
                    return true;
                }else{ //看目前的IP列表是不需要再循环了
                    return false;
                }
            }
        }
        return false;
    }

    private $ips_list =[
        90 => '101.226.103.0/25',
        91 => '101.226.233.128/25',
        92 => '58.247.206.128/25',
        93 => '182.254.86.128/25',
        95 => '103.7.30.64/26',
        96 => '58.251.80.32/27',
        97 => '183.3.234.32/27',
        98 => '121.51.130.64/27'
    ];

    private $ip_list = [
        0 => '101.226.62.77',
        1 => '101.226.62.78',
        2 => '101.226.62.79',
        3 => '101.226.62.80',
        4 => '101.226.62.81',
        5 => '101.226.62.82',
        6 => '101.226.62.83',
        7 => '101.226.62.84',
        8 => '101.226.62.85',
        9 => '101.226.62.86',
        10 => '101.226.103.59',
        11 => '101.226.103.60',
        12 => '101.226.103.61',
        13 => '101.226.103.62',
        14 => '101.226.103.63',
        15 => '101.226.103.69',
        16 => '101.226.103.70',
        17 => '101.226.103.71',
        18 => '101.226.103.72',
        19 => '101.226.103.73',
        20 => '140.207.54.73',
        21 => '140.207.54.74',
        22 => '140.207.54.75',
        23 => '140.207.54.76',
        24 => '140.207.54.77',
        25 => '140.207.54.78',
        26 => '140.207.54.79',
        27 => '140.207.54.80',
        28 => '182.254.11.203',
        29 => '182.254.11.202',
        30 => '182.254.11.201',
        31 => '182.254.11.200',
        32 => '182.254.11.199',
        33 => '182.254.11.198',
        34 => '59.37.97.100',
        35 => '59.37.97.101',
        36 => '59.37.97.102',
        37 => '59.37.97.103',
        38 => '59.37.97.104',
        39 => '59.37.97.105',
        40 => '59.37.97.106',
        41 => '59.37.97.107',
        42 => '59.37.97.108',
        43 => '59.37.97.109',
        44 => '59.37.97.110',
        45 => '59.37.97.111',
        46 => '59.37.97.112',
        47 => '59.37.97.113',
        48 => '59.37.97.114',
        49 => '59.37.97.115',
        50 => '59.37.97.116',
        51 => '59.37.97.117',
        52 => '59.37.97.118',
        53 => '112.90.78.158',
        54 => '112.90.78.159',
        55 => '112.90.78.160',
        56 => '112.90.78.161',
        57 => '112.90.78.162',
        58 => '112.90.78.163',
        59 => '112.90.78.164',
        60 => '112.90.78.165',
        61 => '112.90.78.166',
        62 => '112.90.78.167',
        63 => '140.207.54.19',
        64 => '140.207.54.76',
        65 => '140.207.54.77',
        66 => '140.207.54.78',
        67 => '140.207.54.79',
        68 => '140.207.54.80',
        69 => '180.163.15.149',
        70 => '180.163.15.151',
        71 => '180.163.15.152',
        72 => '180.163.15.153',
        73 => '180.163.15.154',
        74 => '180.163.15.155',
        75 => '180.163.15.156',
        76 => '180.163.15.157',
        77 => '180.163.15.158',
        78 => '180.163.15.159',
        79 => '180.163.15.160',
        80 => '180.163.15.161',
        81 => '180.163.15.162',
        82 => '180.163.15.163',
        83 => '180.163.15.164',
        84 => '180.163.15.165',
        85 => '180.163.15.166',
        86 => '180.163.15.167',
        87 => '180.163.15.168',
        88 => '180.163.15.169',
        89 => '180.163.15.170',
        94 => '103.7.30.21',
    ];

}
